/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TaskSpyApp.java
 *
 * Created on Jun 22, 2012, 10:16:27 PM
 */
package taskspydelux;

import com.skstudio.network.TaskSpyClient;
import java.awt.event.ItemEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author skstudio
 */
public class TaskSpyApp extends javax.swing.JFrame {

    /** Creates new form TaskSpyApp */
    public TaskSpyApp() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        rootPanel = new uiRootPanel(this);
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        connMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        wndMenuItems = new javax.swing.JMenu();
        sysCPU = new javax.swing.JCheckBoxMenuItem();
        sysCore = new javax.swing.JCheckBoxMenuItem();
        coreFreq = new javax.swing.JCheckBoxMenuItem();
        sysMem = new javax.swing.JCheckBoxMenuItem();
        procCpu = new javax.swing.JCheckBoxMenuItem();
        procThread = new javax.swing.JCheckBoxMenuItem();
        procMem = new javax.swing.JCheckBoxMenuItem();
        ntwkThru = new javax.swing.JCheckBoxMenuItem();
        wifiSignal = new javax.swing.JCheckBoxMenuItem();
        cellThru = new javax.swing.JCheckBoxMenuItem();
        battery = new javax.swing.JCheckBoxMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(720, 320));

        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        rootPanel.setName("rootPanel"); // NOI18N
        rootPanel.setLayout(new javax.swing.BoxLayout(rootPanel, javax.swing.BoxLayout.Y_AXIS));
        jScrollPane1.setViewportView(rootPanel);

        fileMenu.setText("File");

        openMenuItem.setText("Open...");
        openMenuItem.setName("openMenuItem"); // NOI18N
        openMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(openMenuItem);

        connMenuItem.setText("Connect ...");
        connMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(connMenuItem);

        saveMenuItem.setText("Save...");
        saveMenuItem.setEnabled(false);
        saveMenuItem.setName("saveMenuItem"); // NOI18N
        saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(saveMenuItem);

        wndMenuItems.setText("Show/Hide Panels");
        wndMenuItems.setName("wndMenuItems"); // NOI18N
        wndMenuItems.addMenuListener(new javax.swing.event.MenuListener() {
            public void menuSelected(javax.swing.event.MenuEvent evt) {
                wndMenuItemsMenuSelected(evt);
            }
            public void menuDeselected(javax.swing.event.MenuEvent evt) {
            }
            public void menuCanceled(javax.swing.event.MenuEvent evt) {
            }
        });

        sysCPU.setSelected(true);
        sysCPU.setText("Total CPU Usage");
        sysCPU.setName("sysCPU"); // NOI18N
        sysCPU.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                sysCPUItemStateChanged(evt);
            }
        });
        wndMenuItems.add(sysCPU);

        sysCore.setSelected(true);
        sysCore.setText("CPU Usage Per Core");
        sysCore.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                sysCoreItemStateChanged(evt);
            }
        });
        wndMenuItems.add(sysCore);

        coreFreq.setSelected(true);
        coreFreq.setText("Core Frequency");
        coreFreq.setName("coreFreq"); // NOI18N
        coreFreq.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                coreFreqItemStateChanged(evt);
            }
        });
        wndMenuItems.add(coreFreq);

        sysMem.setSelected(true);
        sysMem.setText("System Memory");
        sysMem.setName("sysMem"); // NOI18N
        sysMem.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                sysMemItemStateChanged(evt);
            }
        });
        wndMenuItems.add(sysMem);

        procCpu.setSelected(true);
        procCpu.setText("Process CPU Usage");
        procCpu.setName("procCpu"); // NOI18N
        procCpu.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                procCpuItemStateChanged(evt);
            }
        });
        wndMenuItems.add(procCpu);

        procThread.setSelected(true);
        procThread.setText("Process's Threads CPU");
        procThread.setName("procThread"); // NOI18N
        procThread.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                procThreadItemStateChanged(evt);
            }
        });
        wndMenuItems.add(procThread);

        procMem.setSelected(true);
        procMem.setText("Process Memory Delta");
        procMem.setName("procMem"); // NOI18N
        procMem.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                procMemItemStateChanged(evt);
            }
        });
        wndMenuItems.add(procMem);

        ntwkThru.setSelected(true);
        ntwkThru.setText("Network Traffic");
        ntwkThru.setName("ntwkThru"); // NOI18N
        ntwkThru.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                ntwkThruItemStateChanged(evt);
            }
        });
        wndMenuItems.add(ntwkThru);

        wifiSignal.setSelected(true);
        wifiSignal.setText("Wifi Signal Strength");
        wifiSignal.setName("wifiSignal"); // NOI18N
        wifiSignal.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                wifiSignalItemStateChanged(evt);
            }
        });
        wndMenuItems.add(wifiSignal);

        cellThru.setSelected(true);
        cellThru.setText("Cellular Traffic");
        cellThru.setName("cellThru"); // NOI18N
        cellThru.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cellThruItemStateChanged(evt);
            }
        });
        wndMenuItems.add(cellThru);

        battery.setSelected(true);
        battery.setText("Battery");
        battery.setName("battery"); // NOI18N
        battery.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                batteryItemStateChanged(evt);
            }
        });
        wndMenuItems.add(battery);

        fileMenu.add(wndMenuItems);

        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        jMenu1.setText("Help");
        jMenu1.setName("jMenu1"); // NOI18N

        jMenuItem1.setText("About");
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        menuBar.add(jMenu1);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 785, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 785, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 529, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        if (client != null) {
            client.shutdown();
        }
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void sysCPUItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_sysCPUItemStateChanged
        mViewCpu.setVisible(evt.getStateChange() == ItemEvent.SELECTED ? true : false);
    }//GEN-LAST:event_sysCPUItemStateChanged

    private void procMemItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_procMemItemStateChanged
        mViewProcMem.setVisible(evt.getStateChange() == ItemEvent.SELECTED ? true : false);
    }//GEN-LAST:event_procMemItemStateChanged

    private void sysCoreItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_sysCoreItemStateChanged
        mViewCores.setVisible(evt.getStateChange() == ItemEvent.SELECTED ? true : false);
    }//GEN-LAST:event_sysCoreItemStateChanged

    private void coreFreqItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_coreFreqItemStateChanged
        mViewCoreFreq.setVisible(evt.getStateChange() == ItemEvent.SELECTED ? true : false);
    }//GEN-LAST:event_coreFreqItemStateChanged

    private void wndMenuItemsMenuSelected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_wndMenuItemsMenuSelected
        sysCPU.setState(mViewCpu.isVisible());
        sysCore.setState(mViewCores.isVisible());
        coreFreq.setState(mViewCoreFreq.isVisible());
        sysMem.setState(mViewSysMem.isVisible());
        procCpu.setState(mViewProcess.isVisible());
        procMem.setState(mViewProcMem.isVisible());
        procThread.setState(mViewThreads.isVisible());
        ntwkThru.setState(mViewWifiThru.isVisible());
        wifiSignal.setState(mViewWifiSignal.isVisible());
        cellThru.setState(mViewCellular.isVisible());
        battery.setState(mViewBattery.isVisible());
    }//GEN-LAST:event_wndMenuItemsMenuSelected

    private void sysMemItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_sysMemItemStateChanged
        mViewSysMem.setVisible(evt.getStateChange() == ItemEvent.SELECTED ? true : false);
    }//GEN-LAST:event_sysMemItemStateChanged

    private void procCpuItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_procCpuItemStateChanged
        mViewProcess.setVisible(evt.getStateChange() == ItemEvent.SELECTED ? true : false);
    }//GEN-LAST:event_procCpuItemStateChanged

    private void procThreadItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_procThreadItemStateChanged
        mViewThreads.setVisible(evt.getStateChange() == ItemEvent.SELECTED ? true : false);
    }//GEN-LAST:event_procThreadItemStateChanged

    private void ntwkThruItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_ntwkThruItemStateChanged
        mViewWifiThru.setVisible(evt.getStateChange() == ItemEvent.SELECTED ? true : false);
    }//GEN-LAST:event_ntwkThruItemStateChanged

    private void wifiSignalItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_wifiSignalItemStateChanged
        mViewWifiSignal.setVisible(evt.getStateChange() == ItemEvent.SELECTED ? true : false);
    }//GEN-LAST:event_wifiSignalItemStateChanged

    private void cellThruItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cellThruItemStateChanged
        mViewCellular.setVisible(evt.getStateChange() == ItemEvent.SELECTED ? true : false);
    }//GEN-LAST:event_cellThruItemStateChanged

    private void batteryItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_batteryItemStateChanged
        mViewBattery.setVisible(evt.getStateChange() == ItemEvent.SELECTED ? true : false);
    }//GEN-LAST:event_batteryItemStateChanged

    private void connMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connMenuItemActionPerformed
        String s = (String) JOptionPane.showInputDialog(
                this,
                "Please specify remote device IP address",
                "Input IP address",
                JOptionPane.PLAIN_MESSAGE,
                null,
                null,
                "0.0.0.0");
        if (s != null) {
            startSpyClient(s, false);
            saveMenuItem.setEnabled(true);
        }
    }//GEN-LAST:event_connMenuItemActionPerformed

    private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
        JFileChooser fc = new JFileChooser();
        int retVal = fc.showOpenDialog(this);
        if (retVal == JFileChooser.CANCEL_OPTION) return;

        startSpyClient(fc.getSelectedFile().getPath(), true);

        String[] options = {"Cancel"};
        jb.setValue(0);
        jb.setStringPainted(true);
        JOptionPane.showOptionDialog(
                this,
                jb,
                "Loading...",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.PLAIN_MESSAGE,
                null,
                options,
                options[0]);
        if (jb.getValue() != 100) {
            client.shutdown();
            client = null;
        }

        saveMenuItem.setEnabled(false);
    }//GEN-LAST:event_openMenuItemActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        JOptionPane.showMessageDialog(
                this,
                "TaskSpy Delux client\n2011-2012 SK Studio, all rigths reserved.",
                "About",
                JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed
        JFileChooser fc = new JFileChooser();
        int retVal = fc.showSaveDialog(this);
        if (retVal == JFileChooser.CANCEL_OPTION) return;

        client.save(fc.getSelectedFile());
    }//GEN-LAST:event_saveMenuItemActionPerformed
    static TaskSpyClient client = null;

    protected void startSpyClient(String source, boolean load) {
        if (client != null) {
            client.shutdown();
        }
        client = new TaskSpyClient((uiRootPanel) rootPanel, source, load);

        ((uiRootPanel) rootPanel).resetUI();

        mViewCpu.getChart().setViewController(client.getReSysCpu());
        mViewCores.getChart().setViewController(client.getReCores());
        mViewCoreFreq.getChart().setViewController(client.getReCoreSpeed());
        mViewSysMem.getChart().setViewController(client.getReSysMem());
        mViewProcess.getChart().setViewController(client.getReProcess());
        mViewThreads.getChart().setViewController(client.getReProcessThread());
        mViewProcMem.getChart().setViewController(client.getReProcessMem());
        mViewWifiThru.getChart().setViewController(client.getReNetworkThru());
        mViewWifiSignal.getChart().setViewController(client.getReWifi());
        mViewCellular.getChart().setViewController(client.getReCellularThru());
        mViewBattery.getChart().setViewController(client.getReBattery());

        client.start();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TaskSpyApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(TaskSpyApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(TaskSpyApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(TaskSpyApp.class.getName()).log(Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TaskSpyApp tsp = new TaskSpyApp();
                tsp.setVisible(true);
                tsp.setupSubView();
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBoxMenuItem battery;
    private javax.swing.JCheckBoxMenuItem cellThru;
    private javax.swing.JMenuItem connMenuItem;
    private javax.swing.JCheckBoxMenuItem coreFreq;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JCheckBoxMenuItem ntwkThru;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JCheckBoxMenuItem procCpu;
    private javax.swing.JCheckBoxMenuItem procMem;
    private javax.swing.JCheckBoxMenuItem procThread;
    private javax.swing.JPanel rootPanel;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JCheckBoxMenuItem sysCPU;
    private javax.swing.JCheckBoxMenuItem sysCore;
    private javax.swing.JCheckBoxMenuItem sysMem;
    private javax.swing.JCheckBoxMenuItem wifiSignal;
    private javax.swing.JMenu wndMenuItems;
    // End of variables declaration//GEN-END:variables
    uiChartViewProcess mViewProcess = null;
    uiChartViewThreads mViewThreads = null;
    uiChartViewProcMem mViewProcMem = null;
    uiChartViewCpu mViewCpu = null;
    uiChartViewCores mViewCores = null;
    uiChartViewCoreFreq mViewCoreFreq = null;
    uiChartViewSysMem mViewSysMem = null;
    uiChartViewWifiThru mViewWifiThru = null;
    uiChartViewWifiSignal mViewWifiSignal = null;
    uiChartViewCellularThru mViewCellular = null;
    uiChartViewBattery mViewBattery = null;
    public JProgressBar jb = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);

    public void setupSubView() {
        mViewCpu = new uiChartViewCpu();
        mViewCpu.setVisible(true);
        rootPanel.add(mViewCpu);

        mViewCores = new uiChartViewCores();
        mViewCores.setVisible(true);
        rootPanel.add(mViewCores);

        mViewCoreFreq = new uiChartViewCoreFreq();
        mViewCoreFreq.setVisible(true);
        rootPanel.add(mViewCoreFreq);

        mViewSysMem = new uiChartViewSysMem();
        mViewSysMem.setVisible(true);
        rootPanel.add(mViewSysMem);

        mViewProcess = new uiChartViewProcess();
        mViewProcess.setVisible(true);
        rootPanel.add(mViewProcess);

        mViewThreads = new uiChartViewThreads();
        mViewThreads.setVisible(true);
        rootPanel.add(mViewThreads);

        mViewProcMem = new uiChartViewProcMem();
        mViewProcMem.setVisible(true);
        rootPanel.add(mViewProcMem);

        mViewWifiThru = new uiChartViewWifiThru();
        mViewWifiThru.setVisible(true);
        rootPanel.add(mViewWifiThru);

        mViewWifiSignal = new uiChartViewWifiSignal();
        mViewWifiSignal.setVisible(true);
        rootPanel.add(mViewWifiSignal);

        mViewCellular = new uiChartViewCellularThru();
        mViewCellular.setVisible(true);
        rootPanel.add(mViewCellular);

        mViewBattery = new uiChartViewBattery();
        mViewBattery.setVisible(true);
        rootPanel.add(mViewBattery);

        validate();
    }
}
